package my.example.samplegridview;

import my.example.samplegridview.modal.user;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class loginprompt extends Activity{
	
	loginprompt _self;
	public static int activitycode = 900;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginprompt);
		_self = this;
		Button cancelbutton = (Button) findViewById(R.id.loginpromptcancel);
		cancelbutton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				//From Intent
				Intent fromIntent = new Intent();
				fromIntent.putExtra("isLoginSuccess", false);
				
				_self.setResult(activitycode,fromIntent);
				_self.finish();
			}
			
		});
		
		Button submitbutton = (Button) findViewById(R.id.loginpromptsubmit);
		submitbutton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				EditText et = (EditText) findViewById(R.id.loginpromptemailaddress);
				String emailadd = et.getText().toString();
				et = (EditText) findViewById(R.id.loginpromptpassword);
				String password = et.getText().toString();
				
				user u = currentuserdetail.temporaryUserLookup(emailadd, password);
				
				if(u != null){
					currentuserdetail.currentuser = u;
					
					if(u.getType().compareTo("superadmin") == 0){
						//To Intent
						Intent toIntent = new Intent(_self,adminmenu.class);
						
						//From Intent
						Intent fromIntent = new Intent();
						fromIntent.putExtra("isLoginSuccess", true);
						
						_self.startActivity(toIntent);
						_self.setResult(activitycode,fromIntent);
						_self.finish();
					}
					else{
						//To Intent
						Intent toIntent = new Intent(_self,menu.class);
						
						//From Intent
						Intent fromIntent = new Intent();
						fromIntent.putExtra("isLoginSuccess", true);
						
						_self.startActivity(toIntent);
						_self.setResult(activitycode,fromIntent);
						_self.finish();
					}
					
				}
				else{
					Toast.makeText(_self, "Failed to login. Please check your email address/password.", Toast.LENGTH_SHORT).show();
				}
			}
			
		});
		
	}
}
